package xyz.csmcool.imoss;

import io.minio.*;
import io.minio.errors.MinioException;
import xyz.csmcool.imoss.config.PearlMinioClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class FileUploader {
  public static void main(String[] args)
      throws IOException, NoSuchAlgorithmException, InvalidKeyException {
    try {
      // Create a minioClient with the MinIO server playground, its access key and secret key.
      MinioClient minioClient =
          MinioClient.builder()
              .endpoint("http://img2.csmcool.top:9002")
              .credentials("CwpeL7wIuj9H4rV3", "VRltrE1XKLhI8u0ffYhTAs6eAIleDZW7")
              .build();

      // Make 'asiatrip' bucket if not exist.
      boolean found =
          minioClient.bucketExists(BucketExistsArgs.builder().bucket("public").build());
      if (!found) {
        // Make a new bucket called 'asiatrip'.
        minioClient.makeBucket(MakeBucketArgs.builder().bucket("public").build());
      } else {
        System.out.println("Bucket 'asiatrip' already exists.");
      }

      PearlMinioClient pearlMinioClient = new PearlMinioClient(minioClient);
      File file = new File("E:/shushu.jpg");
      InputStream inputStream = new FileInputStream(file);
      pearlMinioClient.putObject(
              PutObjectArgs.builder().bucket("public").object("1.jpg").stream(
                              inputStream, inputStream.available(), -1)
                      .build());
      System.out.println(
          "'E:/shushu.jpg' is successfully uploaded as "
              + "asiaphotos-2015.jpg' to bucket 'asiatrip'.");
    } catch (MinioException e) {
      System.out.println("Error occurred: " + e);
      System.out.println("HTTP trace: " + e.httpTrace());
    }
  }
}
